/**
 * @Title: HttpTask.java
 * @Package com.hxd.love.api.task
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 谢会超
 * @date 2015-8-12 下午2:00:34
 * @version V1.0
 */

package net.bblot.lottery.api.task;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import net.bblot.lottery.api.ApiResponseListener;
import net.bblot.lottery.api.http.HttpConnect;
import net.bblot.lottery.api.result.ExcuterResult;
import net.bblot.lottery.api.result.HttpResult;
import net.bblot.lottery.util.Logger;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;

import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;


/**
 * @author 谢会超
 * @ClassName: HttpTask
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2015-8-12 下午2:00:34
 */

public class HttpTask extends FutureTask<ExcuterResult> {

    private final static String TAG = "HttpTask";
    public static final int RESULT_CODE_OK = 1;
    public static final int RESULT_CODE_FAILUR = -1;

    public final static int HTTP_REQEST_TYPE_GET = 1;
    public final static int HTTP_REQEST_TYPE_POST = 2;
    public final static int HTTP_REQEST_TYPE_PUT = 3;
    public final static int HTTP_REQEST_TYPE_DELETE = 4;

    public static Handler getHttpHandler(final ApiResponseListener listener) {
        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case RESULT_CODE_OK:

                        HttpResult hr = (HttpResult) msg.obj;
                        Logger.d(TAG, "63 code=" + hr.getSc() + "hr.getdate" + hr.getDate());
                        ExcuterResult er = ExcuterResult.parseErrorFromHttpResultCode(hr);
                        if (hr.getSc() == HttpStatus.SC_OK
                                || hr.getSc() == HttpStatus.SC_CREATED || hr.getSc() == HttpStatus.SC_PARTIAL_CONTENT) {
                            listener.onGetResult(RESULT_CODE_OK + "", er);
                        } else {
                            listener.onWrongResponse(RESULT_CODE_FAILUR + "", er);
                        }
                        break;
                    case RESULT_CODE_FAILUR:
                        HttpResult hr2 = (HttpResult) msg.obj;
                        ExcuterResult er2 = ExcuterResult.parseErrorFromHttpResultCode(hr2);
                        listener.onWrongResponse(RESULT_CODE_FAILUR + "", er2);
                        break;
                }
            }

        };
        return handler;
    }

    public static HttpTask getTask(final Handler handler,
                                   final Context context, final String url, final int http_type,
                                   final List<NameValuePair> qurryParams,
                                   final List<NameValuePair> bodyParams, final List<File> file_array, final String file_type) {
        Logger.d(TAG, "url=" + url + "\nqurryParams" + qurryParams
                + "\nbodyParams" + bodyParams + "\nfile_array" + file_array);
        Callable<ExcuterResult> call = new Callable<ExcuterResult>() {

            @Override
            public ExcuterResult call() throws Exception {
                ExcuterResult er = new ExcuterResult();
                HttpResult result = null;
//				Logger.d(TAG, "http_type" + http_type + "101url=" + url
//						+ "\nqurryParams" + qurryParams + "\nbodyParams"
//						+ bodyParams + "\nfile_array" + file_array);
                result = HttpConnect.request(context, url, http_type,
                        qurryParams, bodyParams, file_array, file_type);
                // Logger.d(TAG, "106url=" + url + "\nqurryParams" + qurryParams
                // + "\nbodyParams" + bodyParams + "\nfile_array" + file_array);
                Message msg = new Message();
                msg.obj = result;
                if (result.getSc() == HttpStatus.SC_OK || result.getSc() == HttpStatus.SC_PARTIAL_CONTENT) {
                    msg.what = RESULT_CODE_OK;
                } else {
                    msg.what = RESULT_CODE_FAILUR;
                }
                handler.sendMessage(msg);
                return er;
            }
        };
        return new HttpTask(call);
    }

    public static HttpTask getPostTask(final Handler handler,
                                       final Context context, final String url, final String content) {

        Callable<ExcuterResult> call = new Callable<ExcuterResult>() {

            @Override
            public ExcuterResult call() throws Exception {
                ExcuterResult er = new ExcuterResult();
                HttpResult result = null;
                result = HttpConnect.request(context, url, content);
                Message msg = new Message();
                msg.obj = result;
                if (result.getSc() == HttpStatus.SC_OK || result.getSc() == HttpStatus.SC_PARTIAL_CONTENT) {
                    msg.what = RESULT_CODE_OK;
                } else {
                    msg.what = RESULT_CODE_FAILUR;
                }
                handler.sendMessage(msg);
                return er;
            }
        };
        return new HttpTask(call);
    }

    private HttpTask(Callable<ExcuterResult> callable) {
        super(callable);
    }

}

